// ext-grid.2.x
Ext.onReady(function(){
  Ext.lib.Ajax.defaultPostHeader += '; charset=utf-8';//处理ie提交中文乱码问题     
  // 定义一个checkBox  
  var sm = new Ext.grid.CheckboxSelectionModel();  
  sm.handleMouseDown = Ext.emptyFn;
  // 定义一个ColumnModel，表头中有61列
  var cm = new Ext.grid.ColumnModel([
            sm,
  {header:'客户AccountID' ,dataindex:'idtclient'},
  {header:'组织名称'      ,dataindex:'customername'},
  {header:'组织代码'      ,dataindex:'customercode'},
  {header:'客户编码'      ,dataindex:'accountcode'},
  {header:'客户名称'      ,dataindex:'accountname'},
  {header:'法人名称'      ,dataindex:'corporator'},
  {header:'法人生份证号码',dataindex:'corporatorcode'},
  {header:'公司地址'      ,dataindex:'addresscop'},
  {header:'销售部地址'    ,dataindex:'addresssale'},
  {header:'仓库地址'      ,dataindex:'addressstock'},
  {header:'法人地址'      ,dataindex:'addresstran'},
  {header:'联系人'        ,dataindex:'linker'},
  {header:'联系电话'      ,dataindex:'linkertel'},
  {header:'联系人手机'    ,dataindex:'linkermobile'},
  {header:'联系人传真'    ,dataindex:'linkerfax'},
  {header:'联系人Email'   ,dataindex:'email'},
  {header:'税号'          ,dataindex:'taxno'},
  {header:'银行帐号'      ,dataindex:'accno'},
  {header:'开户银行'      ,dataindex:'bno'},                  
  {header:'开户银行地址'  ,dataindex:'baddr'},                  
  {header:'第二开户银行'  ,dataindex:'bno1'},                  
  {header:'第二开户银行地',dataindex:'baddr1'},                  
  {header:'第二银行帐号'  ,dataindex:'accno1'},                  
  {header:'总公司地址'    ,dataindex:'companyaddress'},            
  {header:'主管业务员 '   ,dataindex:'pname'},                  
  {header:'信用额度'      ,dataindex:'credit'},                  
  {header:'信誉评估'      ,dataindex:'creevaluate'},               
  {header:'企业性质'      ,dataindex:'kind'},                  
  {header:'注册资本'      ,dataindex:'regmoney'},                  
  {header:'企业规模'      ,dataindex:'scope'},                  
  {header:'未付款总额 '   ,dataindex:'nopay'},                  
  {header:'发票标题'      ,dataindex:'title'},                  
  {header:'备注'          ,dataindex:'remark'},                  
  {header:'支付方式'      ,dataindex:'paykind'},                  
  {header:'上次结帐日期 ' ,dataindex:'jzdate'},                  
  {header:'结帐周期(天) ' ,dataindex:'jzperiod'},                  
  {header:'考察评估意见'  ,dataindex:'assess'},                  
  {header:'现经营产品'    ,dataindex:'product'},                  
  {header:'老板背景'      ,dataindex:'fancy'},                  
  {header:'客户等级'     ,dataindex:'clikind'},                  
  {header:'修改人'        ,dataindex:'modipid'},                  
  {header:'修改时间'      ,dataindex:'modidate'},                  
  {header:'财务编号'      ,dataindex:'cwno'},                  
  {header:'财务名称'      ,dataindex:'cwname'},                  
  {header:'财务地区'      ,dataindex:'cwzone'},                  
  {header:'财务结帐日期'  ,dataindex:'cwjzdate'},                  
  {header:'财务应收款'    ,dataindex:'cwjzcredit'},                
  {header:'信用等级'      ,dataindex:'cregrade'},                  
  {header:'邮编'          ,dataindex:'postalcode'},                
  {header:'国家'          ,dataindex:'country'},                  
  {header:'省份'          ,dataindex:'province'},                  
  {header:'县市'          ,dataindex:'county'},                  
  {header:'乡'            ,dataindex:'states'},                  
  {header:'村'            ,dataindex:'status'}                  
]);
 /* ArrayReader
  * ds要对应两个部分：proxy和reader。proxy告诉我们从哪里获得数据，reader告诉我们如何解析这个数据。
  * Ext.data.MemoryProxy，它是专门用来解析js变量的。你可以看到，我们直接把data作为参数传递进去了。
  */
 var ds = new Ext.data.Store({
   baseParams : {trancode : '11110'},
  proxy: new Ext.data.HttpProxy({url:'../'+WebServiceUrl}),
  reader: new Ext.data.JsonReader({
   totalProperty: 'totalProperty',
   root: 'root'
   }, [
   {name: 'idtclient'},
   {name: 'customername'},
   {name: 'customercode'},
   {name: 'accountcode'},
   {name: 'accountname'},
   {name: 'corporator'},
   {name: 'corporatorcode'},
   {name: 'addresscop'},
   {name: 'addresssale'},
   {name: 'addressstock'},
   {name: 'addresstran'},
   {name: 'linker'},
   {name: 'linkertel'},
   {name: 'linkermobile'},
   {name: 'linkerfax'},
   {name: 'email'},
   {name: 'taxno'},
   {name: 'accno'},
   {name: 'bno'},
   {name: 'baddr'},
   {name: 'bno1'},
   {name: 'baddr1'},
   {name: 'accno1'},
   {name: 'company_address'},
   {name: 'pname'},
   {name: 'credit'},
   {name: 'creevaluate'},
   {name: 'kind'},
   {name: 'regmoney'},
   {name: 'scope'},
   {name: 'nopay'},
   {name: 'title'},
   {name: 'remark'},
   {name: 'paykind'},
   {name: 'jzdate'},
   {name: 'jzperiod'},
   {name: 'assess'},
   {name: 'product'},
   {name: 'fancy'},
   {name: 'clikind'},
   {name: 'modipid'},
   {name: 'modidate'},
   {name: 'cwno'},
   {name: 'cwname'},
   {name: 'cwzone'},
   {name: 'cwjzdate'},
   {name: 'cwjzcredit'},
   {name: 'cregrade'},
   {name: 'postal_code'},
   {name: 'country'},
   {name: 'province'},
   {name: 'county'},
   {name: 'states'},
   {name: 'status'}
  ])  
 });   
  
 /* 装配 */
 /* 
  注意：Ext.grid.Grid的第一个参数是渲染的id，
  对应在html里应该有一个 <div id="grid"></div>的东西，这样grid才知道要把自己画到哪里。
  创建完grid以后，还要用grid.render()方法，让grid开始渲染，这样才能显示出来。
 */

 cm.defaultSortable   =   true;  // 设置列可排序

 var grid = new Ext.grid.GridPanel({
  el: 'grid',
  ds: ds,
  cm: cm, 
  loadMask :{msg:'正在加载数据，请稍候。。。'}, 
  // 定义分页
  bbar: new Ext.PagingToolbar({
   pageSize: App.pageSize,
   store: ds,
   displayInfo: true,
   displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
   emptyMsg: "没有记录"
     }),
  sm: sm,
			title : "客户管理",
			// inline toolbars
			tbar : [{
				text : '添加',
				tooltip : '添加',
				handler : doAdd,
				iconCls : 'add'
			}, '-', {
				text : '修改',
				tooltip : '修改',
				handler : doEdit,
				iconCls : 'option'
			}, '-', {
				text : '删除',
				tooltip : '删除',
				handler : doDel,
				iconCls : 'remove'
			}],
			height : 466
		});
  //双击事件
   grid.on('rowdblclick', function(grid, rowIndex, e) {
			new App.EditClient(ds, grid, rowIndex);
		}); 
 //单击事件
 grid.on('rowclick', function(grid,t) { 
  if(sm.isSelected(t)) 
   sm.deselectRow(t); 
  else 
   sm.selectRow(t,true); 
 }); 
 grid.render();
 ds.load({params:{start:0, limit:App.pageSize}});
 
 grid.render();
 //添加
 function doAdd(){
 new App.AddClient(ds);
  }
  /**编辑**/
  function doEdit() {
     var rows = grid.getSelectionModel().getSelections();// 返回值为 Record 数组 
     if (rows.length == 0 || rows.length >= 2) {
      Ext.MessageBox.alert('警告', '修改时请双击一条数据!');
     } else {      
      //修改操作      
     } 
    }
  		/**删除**/
		function doDel() {
					var rows = grid.getSelectionModel().getSelections();// 返回值为 Record 数组 
					if (rows.length == 0) {
						Ext.MessageBox.alert('警告', '最少选择一条信息，进行删除!');
					} else {
						Ext.MessageBox.confirm('提示框', '您确定要进行该操作？', function(btn) {
							if (btn == 'yes') {
								if (rows) {
									for (var i = 0; i < rows.length; i++) {
										//删除]
										Ext.Ajax.request({
											url : '../' + WebServiceUrl,
											params : {
												trancode : '11114',
												id : rows[i].get("idt_client")
											},
											success : function(result) {
												json = Ext.util.JSON
														.decode(result.responseText);
												if (json.success) {
													Ext.MessageBox.alert('操作成功',
															json.message);
												} else {
													Ext.MessageBox.alert('操作失败',
															json.message);
												}
											}
										});
									}
								}
							}
							ds.load({
								params : {
									start : 0,
									limit : App.pageSize
								}
							});
						})
					}	
				}
	    
})
